package engine

import (
	"fmt"
	"gitee.com/volvoxc60/go-crawler/fetcher"
	"log"
)

func Run(seeds ...Request) {

	var requests []Request

	for _, r := range seeds {
		requests = append(requests, r)
	}
	for len(requests) > 0 {
		r := requests[0]
		requests = requests[1:]
		fmt.Printf("req url is %s\n", r.Url)
		body, err := fetcher.Fetch(r.Url)
		if err != nil {
			log.Printf("fetch url failed,url is%s,err is %s\n", r.Url, err)
			continue
		}
		parseResult := r.ParseFunc(body)

		if len(parseResult.Items) <= 0 {
			log.Fatalf("url get empty %s,parse fun is %#v\n", r.Url, r.ParseFunc)
		}
		requests = append(requests, parseResult.Requests...)
		for k, item := range parseResult.Items {

			log.Printf("item is %s,k is %d\n", item, k)

			log.Printf("Requests len  is %d\n", len(parseResult.Requests))

			if len(parseResult.Items) != len(parseResult.Requests) {
				log.Printf("not equal items %d vs requests%d ", len(parseResult.Items), len(parseResult.Requests))
			} else {
				log.Printf(" ✔️equal items %d vs requests%d ", len(parseResult.Items), len(parseResult.Requests))
			}
			//log.Printf("next req parseFunc  is %v\n", parseResult.Requests[k].ParseFunc)
			//log.Println("=================")
		}

	}
}
